// pages/goods-list/goods-list.js
import {search} from '../../api/servers'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
      {
        id:0,
        value:'综合',
        isActive:true
      },
      {
        id:1,
        value:'销量',
        isActive:false
      },
      {
        id:2,
        value:'价格',
        isActive:false
      }
    ],
    goodsList:[]
  },
  //接口要的参数
  QueryParams:{
    query:'',
    cid:'',
    pagenum:1,
    pagesize:10
  },
  totalPages:1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.QueryParams.cid=options.cid||5;
    this.getSearchData();
  },
  async getSearchData(){
    const result=await search(this.QueryParams);
    const total=result.message.total;
    this.totalPages=Math.ceil(total/this.QueryParams.pagesize);//计算总页数
    console.log('totalPages',this.totalPages);
    
    if(result.meta.status===200){
      this.setData({
        //拼接数组
        goodsList:[...this.data.goodsList,...result.message.goods]
      })
    }
  },
  onPullDownRefresh() {
    // Do something when pull down.
    console.log('下拉刷新');
    //重置数组
    this.setData({
      goodsList:[]
    })
    //重置页码
    this.QueryParams.pagenum=1;
    //发送请求
    this.getSearchData().then(()=>{
      wx.stopPullDownRefresh()
    });
  },
  onReachBottom(){
    console.log('触底');
    //判断还有没有下一页
    if(this.QueryParams.pagenum>=this.totalPages){
      //没有下一页
      console.log('没有下一页');
      wx.showToast({
        title: '没有下一页数据了',
        icon:'none'
      })
    }else{
      console.log('还有下一页');
      this.QueryParams.pagenum++;
      this.getSearchData()
    }
  },
  handleTabsItemChange(e){
    console.log('eindex',e);
    const index=e.detail
    let {tabs}=this.data;
    tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false)
    this.setData({
      tabs
    })
  }
 

})